package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.ArticleMessageAdapter;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.ArticleMessage;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 时间：2020/5/24 21:19
 * 功能：信息详情
 */

public class ArticleDetailsActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.profile_image)
    CircleImageView profileImage;
    @BindView(R.id.content)
    TextView content;

    private ArticleMessage am;
    private LevelListDrawable mDrawable = new LevelListDrawable();

    private ArticleMessageAdapter messageAdapter;
    private List<ArticleMessage> lists;
    private RecyclerView re_doctor;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_article_details;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);
        ButterKnife.bind(this);
        am = (ArticleMessage) this.getIntent().getSerializableExtra("am");

        Log.e("范德萨发发","放到"+am.getTitle());
        title.setText(am.getTitle());
        content.setText(Html.fromHtml(am.getContent(), Html.FROM_HTML_MODE_COMPACT, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(final String source) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        mDrawable.addLevel(0, 0, getResources().getDrawable(R.mipmap.ic_launcher));
                        mDrawable.setBounds(0, 0, 200, 200);

                        Bitmap bitmap;
                        try {
                            bitmap = BitmapFactory.decodeStream(new URL(source).openStream());
                            Message msg = handler.obtainMessage();
                            msg.what = 1123;
                            msg.obj = bitmap;
                            handler.sendMessage(msg);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                return mDrawable;
            }
        }, null));


        messageAdapter = new ArticleMessageAdapter(mActivity);
        re_doctor = findViewById(R.id.re_doctor);
        re_doctor.setLayoutManager(new LinearLayoutManager(mActivity));
        re_doctor.setAdapter(messageAdapter);
        lists = new ArrayList<>();
        messageAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                //目前Activity→目标Activity
//                Log.e("范德萨发发","放到"+lists.get(listPosition).getTitle());
                Intent intent = new Intent(mActivity, ArticleDetailsActivity.class);
                intent.putExtra("am",lists.get(listPosition));
                startActivity(intent);
            }
        });
        getData();
    }
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){


                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray array = object.getJSONArray("data");

                        Gson gson = new Gson();
                        for (int i = 0; i<array.length();i++){

                            JSONObject jsonObject = array.getJSONObject(i);
                            ArticleMessage articleMessage = gson.fromJson(jsonObject.toString(),ArticleMessage.class);
                            lists.add(articleMessage);
                        }
                        messageAdapter.setListAndNotifyDataSetChanged(lists);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;
                case 1123:
                    Bitmap bitmap = (Bitmap)msg.obj;
                    BitmapDrawable drawable = new BitmapDrawable(null, bitmap);
                    mDrawable.addLevel(1, 1, drawable);
                    mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    mDrawable.setLevel(1);

                    CharSequence charSequence = content.getText();
                    content.setText(charSequence);
                    content.invalidate();
                    break;

            }

        }
    };

    public void getData() {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("data[row]",1+"");
        params.put("data[number]",10+"");
        params.put("data[client]",1+"");
        params.put("cmd","getAllArticle");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/article/", params, handler, 1, 404);

    }
}

